package work.supervision.platform.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import work.supervision.platform.domain.SearchParam;
import work.supervision.platform.util.DateUtils;

import java.util.*;

@Repository
public class WorkTaskDao {

    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public List<Map> list(SearchParam param){
        return namedParameterJdbcTemplate.queryForList(sqlBuilder(param,true),dealWithQueryParam(voToMap(param),param));
    }

    public int count(SearchParam param){
        String sql = "select count(id) from (" + sqlBuilder(param,false) + ") a";
        return  namedParameterJdbcTemplate.queryForObject(sql,dealWithQueryParam(voToMap(param),param),Integer.class);
    }
    private String sqlBuilder(SearchParam param,boolean isPage){
        StringBuilder sql = new StringBuilder("select * from work_task where 1=1 ");
        //首先处理时间
        if(!StringUtils.isEmpty(param.getSwsjDate())){
            sql.append("and swsj=:swsj ");
        }
        if(!StringUtils.isEmpty(param.getCreateDate())){
            sql.append("and screate=:screate ");
        }
        if(!StringUtils.isEmpty(param.getScbssjDate())){
            sql.append("and scbssj=:scbssj ");
        }
        if(!StringUtils.isEmpty(param.getSbjsjDate())){
            sql.append("and bjsj>=:sbjsj ");
        }
        if(!StringUtils.isEmpty(param.getEbjsjDate())){
            sql.append("and bjsj<=:ebjsj ");
        }

        if(!StringUtils.isEmpty(param.getSwzh())){
            sql.append("and swzh like '%' :swzh '%' ");
        }
        if(!StringUtils.isEmpty(param.getLwdw())){
            sql.append("and lwdw like '%' :lwdw '%' ");
        }
        if(!StringUtils.isEmpty(param.getLwzh())){
            sql.append("and lwzh like '%' :lwzh '%' ");
        }
        if(!StringUtils.isEmpty(param.getWjbt())){
            sql.append("and wjbt like '%' :wjbt '%' ");
        }
        if(!StringUtils.isEmpty(param.getLdps())){
            sql.append("and ldps like '%' :ldps '%' ");
        }
        if(!StringUtils.isEmpty(param.getCbbm())){
            sql.append("and cbbm=:cbbm ");
        }
        if(!StringUtils.isEmpty(param.getBlzt())){
            sql.append("and blzt=:blzt ");
        }
        if(!StringUtils.isEmpty(param.getBljd())){
            sql.append("and bljd like '%' :bljd '%' ");
        }
        if(!StringUtils.isEmpty(param.getCbr())){
            sql.append("and cbr like '%' :cbr '%' ");
        }
        if(!StringUtils.isEmpty(param.getBz())){
            sql.append("and bz like '%' :bz '%' ");
        }
        if(isPage){
            sql.append(" order by  screate desc ");
            Integer start = (param.getPageNum()-1)*param.getPageSize();
            sql.append(" limit " + start+ "," + param.getPageSize());
        }
        return sql.toString();
    }

    public Map dealWithQueryParam(Map map,SearchParam param){
        if(!StringUtils.isEmpty(param.getCreateDate())){
            map.put("screate",param.getCreateDate());
        }
        if(!StringUtils.isEmpty(param.getSbjsjDate())){
            map.put("sbjsj",param.getSbjsjDate());
        }
        if(!StringUtils.isEmpty(param.getEbjsjDate())){
            map.put("ebjsj",param.getEbjsjDate());
        }
        return map;
    }



    public SearchParam selectById(Integer id){
        Map param = new HashMap();
        param.put("id",id);
        Map map = namedParameterJdbcTemplate.queryForMap("select *  from work_task where id=:id", param);
        return mapToVo(map);
    }

    public int add(SearchParam wt){
        String sql = "insert into work_task(swsj,swzh,lwdw,lwzh,wjbt,ldps,cbbm,scbssj,bjsj,blzt,bljd,cbr,bz,screate) " +
                "values (:swsj,:swzh,:lwdw,:lwzh,:wjbt,:ldps,:cbbm,:scbssj,:bjsj,:blzt,:bljd,:cbr,:bz,:screate)";
        return namedParameterJdbcTemplate.update(sql,voToMap(wt));
    }

    public int update(SearchParam wt){
        String sql = "update  work_task set swsj=:swsj,swzh=:swzh,lwdw=:lwdw,lwzh=:lwzh,wjbt=:wjbt,ldps=:ldps,cbbm=:cbbm,scbssj=:scbssj,bjsj=:bjsj,blzt=:blzt,"+
        "bljd=:bljd,cbr=:cbr,bz=:bz where id=:id ";
        return namedParameterJdbcTemplate.update(sql,voToMap(wt));
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    public int delete(String ids){
        Map param = new HashMap();
        int rows = 0;
        if(ids!=null){
            for (String id:ids.split(",")) {
                param.put("id",id);
                rows+=namedParameterJdbcTemplate.update("delete from work_task where id=:id",param);
            }
        }
        return rows;
    }

    /**
     * 根据状态查询部门待办事件
     * @return
     */
    public List<Map> queryDeptNumByStatus(String status){
        Map param = new HashMap();
        param.put("blzt",status);
        List list = namedParameterJdbcTemplate.queryForList("select cbbm,count(*) num from work_task where blzt=:blzt group by cbbm order by num desc", param);
        return list==null?new ArrayList<>():list;
    }



    private SearchParam mapToVo(Map map){
        SearchParam vo = new SearchParam();
        vo.setSwsjDate(map.get("swsj")==null?"":DateUtils.formatDate((Date) map.get("swsj")));
        vo.setScbssjDate(map.get("scbssj")==null?"":DateUtils.formatDate((Date) map.get("scbssj")));
        vo.setBjsjDate(map.get("bjsj")==null?"":DateUtils.formatDate((Date) map.get("bjsj")));
        vo.setCreateDate(map.get("screate")==null?"":DateUtils.formatDate((Date) map.get("screate")));
        vo.setSwzh(map.get("swzh") == null?"":map.get("swzh").toString());
        vo.setLwdw(map.get("lwdw") == null?"":map.get("lwdw").toString());
        vo.setLwzh(map.get("lwzh") == null?"":map.get("lwzh").toString());
        vo.setWjbt(map.get("wjbt") == null?"":map.get("wjbt").toString());
        vo.setLdps(map.get("ldps") == null?"":map.get("ldps").toString());
        vo.setCbbm(map.get("cbbm") == null?"":map.get("cbbm").toString());
        vo.setBlzt(map.get("blzt") == null?"":map.get("blzt").toString());
        vo.setBljd(map.get("bljd") == null?"":map.get("bljd").toString());
        vo.setCbr(map.get("cbr") == null?"":map.get("cbr").toString());
        vo.setBz(map.get("bz") == null?"":map.get("bz").toString());
        vo.setId(map.get("id") == null?0:Integer.valueOf(map.get("id").toString()));
        return vo;
    }
    private Map voToMap(SearchParam wt){
        Map param = new HashMap<>();
        param.put("swzh",wt.getSwzh());
        param.put("lwdw",wt.getLwdw());
        param.put("lwzh",wt.getLwzh());
        param.put("wjbt",wt.getWjbt());
        param.put("ldps",wt.getLdps());
        param.put("cbbm",wt.getCbbm());
        param.put("blzt",wt.getBlzt());
        param.put("bljd",wt.getBljd());
        param.put("cbr",wt.getCbr());
        param.put("bz",wt.getBz());
        param.put("swsj",StringUtils.isEmpty(wt.getSwsjDate())?null:wt.getSwsjDate());
        param.put("scbssj",StringUtils.isEmpty(wt.getScbssjDate())?null:wt.getScbssjDate());
        param.put("bjsj",StringUtils.isEmpty(wt.getBjsjDate())?null:wt.getBjsjDate());
        param.put("screate",DateUtils.formatDate(new Date()));
        param.put("id",wt.getId());
        return param;
    }

}
